package br.edu.ifpi.aula0204.collections.dao.impl;

import static org.junit.Assert.*;

import java.sql.Connection;
import java.sql.SQLException;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import br.edu.ifpi.aula0204.collections.dao.ConnectionFactory;
import br.edu.ifpi.aula0204.collections.entity.Participante;

public class ParticipanteJDBCDAOTest {

	private static Connection conn;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		conn = ConnectionFactory.getConnection();
		conn.prepareStatement("DELETE FROM participante").executeUpdate();
	}

	@Test
	public void testFindByCPF() {
		Participante p = new Participante("2484", "Paulo", "223322",
				"Estudante");
		ParticipanteJDBCDAO dao = new ParticipanteJDBCDAO();
		assertNull(dao.findByCpf(p.getCpf()));
	}

	@Test
	public void testFindById() {
		ParticipanteJDBCDAO dao = new ParticipanteJDBCDAO();
		assertNull(dao.find(150));
	}

	@Test
	public void testInsert() {
		Participante p = new Participante("123", "Paulo", "223322", "Estudante");
		ParticipanteJDBCDAO dao = new ParticipanteJDBCDAO();
		dao.save(p);
		assertTrue(p.getId() > 0);
	}

	@Test
	public void testUpdate() {
		ParticipanteJDBCDAO dao = new ParticipanteJDBCDAO();
		Participante p = dao.findByCpf("123");
		p.setNome("Paulo Chaves");
		p.setFone("99889933");
		dao.save(p);
		p = dao.findByCpf("123");
		assertEquals("Paulo Chaves", p.getNome());
	}

	@Test
	public void testDelete() {
		ParticipanteJDBCDAO dao = new ParticipanteJDBCDAO();
		Participante p = new Participante("3344", "Paulo", "223322",
				"Estudante");
		dao.save(p);
		dao.delete(p.getId());
		assertNull(dao.findByCpf("3344"));
	}

	@AfterClass
	public static void setUpAfterClass() {
		try {
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
